﻿
using IM.Easy.Core.Attributes;
using IM.Easy.Core.Result;
using IM.Easy.Core.Utils;
using IM.Easy.CoreServices.Dto;
using IM.Easy.CoreServices.IServices;
using IM.Easy.Entity;
using IM.Easy.Infrastructure.Query;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// 字典类型服务
    /// </summary>
    [Route("system/dict/data")]
    public class DictDataController : ControllerBase
    {
        private readonly IDictTypeService _dictTypeService;
        private readonly IDictDataService _dictDataService;

        public DictDataController(IDictTypeService dictTypeService, IDictDataService dictDataService)
        {
            _dictTypeService = dictTypeService;
            _dictDataService = dictDataService;
        }

        /// <summary>
        /// 获取字典数据基本信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [Api(Name = "获取字典数据", AllowAnonymous = true)]
        public DictDataDto Get([FromRoute] long id)
        {
            return _dictDataService.GetData(id);
        }

        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("")]
        [Api(Name = "添加字典数据", Perm = "dict:item:add")]
        public long Add([FromBody] DictDataDto dto)
        {
            var id = _dictDataService.Add(dto);
            return id;
        }


        /// <summary>
        /// 更新字典数据
        /// </summary>
        /// <param name="dto"></param>
        [HttpPut]
        [Api(Name = "更新字典数据", Perm = "dict:item:put")]
        public bool Update([FromBody] DictDataDto dto)
        {
            var result = _dictDataService.Update(dto);
            return result;
        }

        /// <summary>
        /// 获取所有字典数据
        /// </summary>
        /// <returns>所有字典数据列表</returns>
        [HttpGet("list")]
        [Api(Name = "获取字典数据列表")]
        public PageResponse<DictDataDto> GetList(DictDataQueryDto query)
        {
            var page = _dictDataService.GetList(query);
            return page;
        }

        /// <summary>
        /// 根据字典类型查询字典数据信息
        /// </summary>
        /// <param name="dictType"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("type/{dictType}")]
        [Api(Name = "获取字典组数据", Desc = "根据字典组获取字典组中的数据", AllowAnonymous = true)]
        public List<DictDataDto> DictType(string dictType)
        {
            var list = _dictDataService.GetByTypes(dictType);
            return list;
        }

        /// <summary>
        /// 删除----支持批量
        /// </summary>
        /// <param name="ids"></param>
        [HttpDelete("bulk")]
        [Api(AllowAnonymous = false, Desc = "批量删除字典值", Name = "批量删除字典值", Perm = "dict:item:bulk:del")]
        public BlukResult BulkDelete([FromBody] params long[] ids)
        {
            var result = new BlukResult();
            foreach (var id in ids)
            {
                var flag = _dictDataService.DeleteById(id);
                if (flag) result.AddSuccess();
                else result.AddFailure(id, $"删除失败");
            }
            return result;
        }

        /// <summary>
        /// 删除----仅支持单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [Api(AllowAnonymous = false, Desc = "单个删除字典值", Name = "删除字典值", Perm = "dict:item:del")]
        public bool Delete([FromRoute] long id)
        {
            var flag = _dictDataService.DeleteById(id);
            return flag;
        }


    }
}
